package com.jash.myutils.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

class ImageTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView img;
    private String url;
    private int id;

    public ImageTask(ImageView img, int id) {
        this.img = img;
        img.setTag(this);
        this.id = id;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        img.setImageResource(id);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            url = params[0];
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            int code = connection.getResponseCode();
            if (code == 200) {
                return BitmapFactory.decodeStream(connection.getInputStream());
            }
        } catch (IOException ignored) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(final Bitmap bitmap) {
        if (bitmap != null) {
            img.setImageBitmap(bitmap);
            ImageUtils.cache.put(url, bitmap);
            //子线程写入文件耗时操作 并压缩
            new Thread(new Runnable() {
                @Override
                public void run() {
                    FileOutputStream fileOutputStream = null;
                    try {
                        fileOutputStream = new FileOutputStream(new File(img.getContext().getExternalCacheDir(),
                                url.replaceAll("/", "")));
                        //压缩比100%
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
                        fileOutputStream.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else {
            img.setImageResource(id);
        }
    }

    @Override
    protected void onCancelled(Bitmap bitmap) {
        if (bitmap != null) {
            ImageUtils.cache.put(url, bitmap);
        }
    }
}
